package com.cxdr.awsips.analysis.processor;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;

import com.cxdr.awsips.analysis.bean.DataType;
import com.cxdr.awsips.analysis.bean.FileInfo;
import com.cxdr.awsips.analysis.config.SystemConfig;
import com.cxdr.awsips.analysis.logger.LogFactory;

public class FileScaner {
	private static Logger logger = com.cxdr.awsips.analysis.logger.LogFactory.makeNewLogger(FileScaner.class);
	private Timer timer;
	private boolean isStop = true;
	private class Scanner extends Thread{
		@Override
		public void run(){
			
		}
	}
	public FileScaner() {
		timer = new Timer();
	}

	public void start() {
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				String sourcePath = com.cxdr.awsips.analysis.config.SystemConfig.get("path.source");
				logger.info("开始扫描目录"+sourcePath+"....");
				File sourcePathDir = new File(sourcePath);
				if(!sourcePathDir.isDirectory()){
					logger.info("目录"+sourcePath+"不存在，请检查源目录");
//					FileScaner.this.cancel();
					return;
				}
				//扫描目录
				File[] files = sourcePathDir.listFiles();
				logger.info("扫描到 "+files.length+"个文件" );
				for(File file:files){
					/*根据文件名进行判断,分别将包文件，更正单站文件放入不同队列
					 * 单站文件直接进行备份
					 */
					
					if(!checkFileName(file)){
						continue;
					}
					FileInfo info = new FileInfo(file);
					if(info.getDataType()==null){
						logger.info("文件类型错误 "+info.getFile().getName());
						continue;
					}
					if (info.getDataType()==DataType.P||info.getDataType()==DataType.U) {
						FileAnalysis.QUEUE.add(info);
					}
				}
			}
		}, 1000,5000);
		isStop = false;
	}
	private boolean checkFileName(File file){
		if(!FilenameUtils.isExtension(file.getName(), "txt")){
			logger.error("文件不是txt错误，删除文件 "+file.getName());
			file.delete();
			return false;
		}
		return true;
	}
	
	public void cancel() {
		timer.cancel();
		isStop = true;
	}

	public boolean isStop() {
		return isStop;
	}

	public void setStop(boolean isStop) {
		this.isStop = isStop;
	}
}
